package services;

import dao.BillInfo_Repository;
import models.BillInfo_F_Entity;
import models.DatabaseExecutionContext;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import play.db.jpa.JPAApi;
import utils.StringToArray;
import vo.BillInfo;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class BillInfo_Repository_JPA implements BillInfo_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public BillInfo_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    public CompletionStage<List<BillInfo>> getBillInfoList(String status, String PhoneNumber, String StartDate, String EndDate, String HWMC, String KHMC, String Status) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {

                String strSql = "select * from f_GetBillInfo('" + PhoneNumber + "','" + StartDate + "','" + EndDate + "','" + HWMC + "','" + KHMC + "','" + status + "')";
                List<BillInfo> BillInfos = new ArrayList<BillInfo>();
                List<BillInfo_F_Entity> list = entityManager.createNativeQuery(strSql, BillInfo_F_Entity.class).getResultList();

                if (CollectionUtils.isNotEmpty(list)) {
                    for (BillInfo_F_Entity entity : list) {
                        List<String> BillInfo = new ArrayList<String>();
                        List<Object> objectList = new ArrayList<Object>();

                        if (entity.getDeliveryPlan() != null) {
                    // System.out.println(entity.getDeliveryPlan());

                            String ary0 = null;
                            String[] ary = StringToArray.convertStrToArray1(entity.getDeliveryPlan());

                            if (ary != null) {
//                          System.out.println("tran20" +entity.getDeliveryPlan());
                                for (int a = 0, b = ary.length; a < b; a++) {
                                    ary0 = ary[a];
//
                                    String[] tran = StringToArray.convertStrToArray(ary0);
                                 //  System.out.println("长度"+ tran .length);
                                    if (tran.length > 15) {
                                        Integer length = tran.length;
                                        String tran0 = "{RowId" + ":" + "\"" + tran[0] + "\"" + " ,";
                                        String tran1 = "DeliveryNo" + ":" + "\"" + tran[1] + "\"" + ",";
                                        String tran2 = "Amount" + ":" + "\"" + tran[2] + "\"" + ",";
                                        String tran3 = "DeliveryPlace" + ":" + "\"" + tran[3] + "\"" + ",";
                                        String tran4 = "WeightId" + ":" + "\"" + tran[4] + "\"" + ",";
                                        String tran5 = "StartDate" + ":" + "\"" + tran[5] + "\"" + ",";
                                        String tran6 = "EndDate" + ":" + "\"" + tran[6] + "\"" + ",";
                                        String tran7 = "TransType" + ":" + "\"" + tran[7] + "\"" + ",";
                                        String tran8 = "ShipName" + ":" + "\"" + tran[8] + "\"" + ",";
                                        String tran9 = "CreateDate" + ":" + "\"" + tran[9] + "\"" + ",";
                                        String tran10 = "CreateBy" + ":" + "\"" + tran[10] + "\"" + ",";
                                        String tran11 = "UpdateDate" + ":" + "\"" + tran[11] + "\"" + ",";
                                        String tran12 = "UpdateBy" + ":" + "\"" + tran[12] + "\"" + ",";
                                        String tran13 = "UnloadPlace" + ":" + "\"" + tran[13] + "\"" + ",";
                                        String tran14 = "CustomerDept" + ":" + "\"" + tran[14] + "\"" + ",";
                                        String tran15 = "CONUSER" + ":" + "\"" + tran[15] + "\"" + ",";

                                        String tran16 = null;
                                        String tran17 = null;
                                        String tran18=null;
                                        if (length >=17) {

                                            tran16 = "DeliveBatchCode" + ":" + "\"" + tran[16] + "\"" + ",";
                                            tran17="FinshAmount" + ":" + "\"" + tran[17] + "\"" + ",";
                                            tran18 = "residueAmount" + ":" + "\"" + tran[18] + "\"" + "}";
                                          //  System.out.println("tran17的值"+ tran[17]);
                                        } else {
                                            tran16 = "DeliveBatchCode" + ":" + "\""  +  "\"" + ",";
                                            tran17="FinshAmount" + ":" + "\""  +  "\"" + ",";
                                            tran18 = "residueAmount" + ":" + "\""  +  "\"" + "}";
                                        }
                                        

                                        String tran19 = tran0 + tran1 + tran2 + tran3 + tran4 + tran5 + tran6 + tran7 + tran8 + tran9 + tran10 + tran11 + tran12 + tran13 + tran14 + tran15 + tran16+tran17+tran18;
                                        BillInfo.add(tran19);


                                    }


                                }
                                String BillInfosCommaSeparated = String.join(",", BillInfo); //列表转为字符串
                                String BillInfoCopy = "[" + BillInfosCommaSeparated.substring(0, BillInfosCommaSeparated.length()) + "]";
                                JSONArray jsonArray = JSONArray.fromObject(BillInfoCopy);//把String转换为json

                                objectList.add(jsonArray);
//
                            }
                        }

                        vo.BillInfo BillInfoPlan = new BillInfo();
                        BillInfoPlan.setContractId(entity.getContractId());
                        BillInfoPlan.setContractNumber(entity.getContractNumber());
                        BillInfoPlan.setBillId(entity.getBillId());
                        BillInfoPlan.setBillNo(entity.getBillNo());
                        BillInfoPlan.setProductName(entity.getProductName());
                        BillInfoPlan.setCustomerName(entity.getCustomerName());
                        BillInfoPlan.setDeliveryType(entity.getDeliveryType());
                        BillInfoPlan.setStartDate(entity.getStartDate());
                        BillInfoPlan.setEndDate(entity.getEndDate());
                        BillInfoPlan.setUnloadPlace(entity.getUnloadPlace());
                        BillInfoPlan.setDeliveryPlace(entity.getDeliveryPlace());
                        BillInfoPlan.setConUser(entity.getConUser());
                        BillInfoPlan.setTransType(entity.getTransType());
                        BillInfoPlan.setDeliverInfo(objectList);
                        BillInfoPlan.setStatus(entity.getStatus());
                        BillInfoPlan.setBillRemain(entity.getBillRemain());
                        BillInfoPlan.setBillAmount(entity.getBillAmount());
                        BillInfos.add(BillInfoPlan);
                    }


                }
                return BillInfos;

            });
        }, executionContext);
    }

}
